const request = require("../../utils/request.js")
const app = getApp()

Component({
  /**
   * 组件的属性列表
   */
  properties: {

    blogId:{
      type:String,
      value:""
    },
    //发布动态的用户id
    userId:{
      type:String,
      value:""
    },
    // userAvator:
    // {
    //   type:String,
    //   value:"../../imgs/service.png",
    // },
    // userName:
    // {
    //   type:String,
    //   value: "用户1",
    // },
    time:
    {
      type:String,
      value:"两小时前"
    },
    essayTitle:
    {
      type:String,
      value:"今天垃圾收集了吗"
    },
    context:{
      type:String,
      value:"今天有收集垃圾吗？今天有收集垃圾吗？今天有收集垃圾吗？今天有收集垃圾吗？"
    },
    photoArrStr:
    {
      type:String,
      value:""
    },
    thumb:
    {
      type:Number,
      value:1000
    },
    comment:
    {
      type:Number,
      value:500
    },
    showComment:
    {
      type:Boolean,
      value:false
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    sysWidth:wx.getSystemInfoSync().windowWidth, //图片宽度
    photoWidth:wx.getSystemInfoSync().windowWidth / 4,
    isthumbed:false,
    thumbId:null,
    //showComment:false,
    commentList:[
      {
        'commentId':'1',
        'nickname':'李博文',
        'content':"真不错哦"
      }
    ],
    myComment:"",
    //发布动态的用户信息
    userAvator:"",
    userName:"",
    photos:[],
    isOrderShare:false,
    curShareOrderObj:null,
    orderType:'',
    deliverOrderIcon:'/imgs/deliverIcon.png',
    appointOrderIcon:'/imgs/indexGrid/doorService.png'
  },
  // 组件声明周期
  lifetimes: {
    attached:async function() {

      if(this.data.photoArrStr!=""&&this.data.photoArrStr.indexOf("Eassyimgs")==-1)
      {
        let curShareOrderObj = JSON.parse(this.data.context)
        let orderType = JSON.parse(this.data.photoArrStr)
        // console.log('订单信息',curShareOrderObj)
        // console.log('订单类型',orderType)

        this.setData({
          isOrderShare:true,
          curShareOrderObj,
          orderType:orderType.orderType
        })
      }
      else
      {
        this.getphotos()
      }
      // console.log(this.data.photos)
  // 在组件实例进入页面节点树时执行
      //console.log(this.data.sysWidth)
      // console.log(this.data.userId)
      /**
       * 查询发布动态的用户信息
       */
      const userId = this.data.userId
      if(userId!="")
      {
        let res = await request.getUserById(userId)
        // console.log(res.data.data.value)
        const userInfo = res.data.data.value
        this.setData({
          userAvator:userInfo.imageUrl,
          userName:userInfo.nickname
        })
      }
      /**
       * 查询该动态用户是否点赞
       */
      let res =await request.thumbInterface(this.data.blogId,this.data.userId,null,"getIsThumb")
      // console.log("点赞信息",res)
      if(res.data.data.value)
      {
        this.setData({
          isthumbed:true,
          thumbId:res.data.data.value.thumbId
        })
      }
      
      this.getCommentList()

    },
    detached: function() {
      // 在组件实例被从页面节点树移除时执行
    },
  },

  /**
   * 组件的方法列表
   */
  methods: {
    /**
     * 获取图片信息
     */
    getphotos()
    {
      // let res =await request.getimgByBlogId(this.data.blogId)
      // console.log("动态的图片列表",res.data.data.value)
      // this.setData({
      //   photos:res.data.data.value
      // })
      let photoArr = this.data.photoArrStr.trim('').split(';')
      let photos=[]
      photoArr.map((item)=>{
        // console.log(item)
        if(item!='')
          // photos.push(app.globalData.imgUrlPre+item)
          {
            request.downloadFile(item).then((res)=>{
              photos.push(res)
              console.log(res)
              if(photos.length==photoArr.length)
              {
                this.setData({
                  photos
                })
              }
            })
          }
      })

    },
      // 点击图片进行大图查看
  LookPhoto: function(e) {
    // console.log(e.currentTarget.dataset.photoindex)
    wx.previewImage({
      current: this.data.photos[e.currentTarget.dataset.photoindex],
      urls: this.data.photos,
    })
  },
          /**
       * 查询该动态的评论
       */
      async getCommentList()
   {
  let res1 = await request.commentInterface(undefined,this.data.blogId,undefined,undefined,undefined,undefined,undefined,"getCommentListByblogId")
      // console.log(res1.data.data.value)
      this.setData({
        commentList:res1.data.data.value,
        comment:res1.data.data.value.length
      })
    },
    
    async onthumb()
    {
      if(!this.data.isthumbed)
      {
        //点赞加1
        //点赞表新增
        // console.log(app.globalData.userInfo.nickName)
        let res = await request.thumbInterface(this.data.blogId,app.globalData.openid,undefined,"addThumb")
        // console.log("新增点赞后",res)
        this.setData({
          thumbId:res.data.data.value.thumbId
        })
        console.log(this.data.thumbId)
        //动态的点赞数修改
        let res1 = await request.communityInterface(this.data.blogId,undefined,undefined,undefined,this.data.thumb+1,undefined,undefined,undefined,"update")
        console.log(res1)

        /**
         * 通知用户被点赞了
         */
       let res2 = await  request.noticeInterface(undefined,`title:"他人消息";content:"来自${app.globalData.userInfo.nickName}的点赞"`,"add")
       console.log(res2.data.data.value)
       const noticeInfo = res2.data.data.value
       let res3 = await request.noticeUserInterface(undefined,undefined,noticeInfo.systemNoticeId,app.globalData.openid,"add")
      } 
      else
      {
        //点赞减1
        //删除点赞表
        let res = await request.deleteThumb(this.data.thumbId)
        this.setData({
          thumbId:null
        })
        // console.log(res)
        //动态点赞数修改
        let res1 = await request.communityInterface(this.data.blogId,undefined,undefined,undefined,this.data.thumb-1,undefined,undefined,undefined,"update")
        // console.log("减少点赞数后",res1)
      }
      this.setData({
        thumb:this.data.thumb + (this.data.isthumbed?-1:1),
        isthumbed:!this.data.isthumbed,
      })
    },
    comment()
    {
      
      this.setData({
        showComment:!this.data.showComment,
      })

      // if(showComment)
      // {

      // }
    },
    getCommentValue(e)
    {
      // console.log(e.detail.value)
      this.setData({
        myComment:e.detail.value
      })
    },
    async addComment()
    {
      var that = this
      //内容审查
      wx.showLoading({
        title: '正在审核内容。。。',
      })
    let contentValid = true
    //调用云函数进行审核
    let checkRes = await wx.cloud.callFunction({
      name: 'contentCheck' ,
      data:{
        'content':that.data.myComment
      } 
    })
    wx.hideLoading()
    //获取状态码  0 》》》正常   87014》》》违规
    const {errCode} = checkRes.result
    if(errCode != 0){
      contentValid=false
      // console.log(contentValid)
      wx.showModal({
        title: '违规提示',
        content: '输入的内容违规',
        showCancel: false,
        confirmColor: '#DC143C',
        success (res) {
          if (res.confirm) {
            that.setData({
              myComment:''
            })
          }
        }
      })
    }     

    if(contentValid)
    {
      // console.log(app.globalData.userInfo)
      let res = await request.commentInterface(undefined,this.data.blogId,this.data.userId,this.data.myComment,app.globalData.userInfo.nickName,undefined,app.globalData.openid,"addComment")
      // console.log(res)
        //动态点赞数修改
        let res1 = await request.communityInterface(this.data.blogId,this.data.commentList.length+1,undefined,undefined,undefined,undefined,undefined,undefined,"update")

      this.getCommentList()

              /**
         * 通知用户被评论了
         */
        let res2 = await  request.noticeInterface(undefined,`title:"他人消息";content:"来自${app.globalData.userInfo.nickName}的评论"`,"add")
        console.log(res2.data.data.value)
        const noticeInfo = res2.data.data.value
        let res3 = await request.noticeUserInterface(undefined,undefined,noticeInfo.systemNoticeId,app.globalData.openid,"add")


      this.setData({
        myComment:""
      })
    }
  }
  }
})
